<!DOCTYPE html>
<html lang="en">
<head xmlns:th="http://www.thymeleaf.org">
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="/css/bootstrap.min.css">




    <script src="/js/jquery-3.6.1.min.js"></script>


</head>
<body>
<table class="table table-bordered">


    <form class="form-inline">
        <div class="input-group" style="padding: 10px">
            <input type="text" class="form-control" id="exampleInputEmail2" placeholder="输入查询的姓名或学号">
            <div class="input-group-btn">
                <button type="button" class="btn btn-primary" onclick="find()">搜索</button>
            </div>
        </div>
    </form>


    <p id="p1">查询结果</p>












<div class="container-fluid">
    这就是最基本的页面！

    <!-- Button trigger modal -->
    <button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal" onclick="preAdd();">
        新增学生
    </button>



    <table class="table table-bordered">
        <thead>
        <tr class="info">
            <th>序号</th>
            <th>学号</th>
            <th>姓名</th>
            <th>性别</th>
            <th>成绩</th>
            <th style="width:100px;">操作</th>
        </tr>
        </thead>
        <tbody id="studentTb" >


        </tbody>
    </table>

</div>
<div class="modal fade" tabindex="-1" role="dialog" id="myModal">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">学生信息</h4>
            </div>
            <div class="modal-body">

                <form id="formStudent">
                    <input hidden="hidden" id="id" name="id"/>
                    <div class="form-group">
                        <label for="name">姓名</label>
                        <input type="text" class="form-control" id="name" name="name" placeholder="姓名">
                    </div>
                    <div class="form-group">
                        <label for="no">学号</label>
                        <input type="text" class="form-control" id="no" name="no" placeholder="学号">
                    </div>
                    <div class="form-group">
                        <label for="password">密码</label>
                        <input type="password" class="form-control" id="password" name="password" placeholder="密码">
                    </div>
                    <div class="form-group">
                        <label for="sex">性别：</label>
                        <select class="form-control" id="sex" name="sex">
                            <option value="1">男</option>
                            <option value="2">女</option>

                        </select>
                    </div>

                    <div class="form-group">
                        <label for="score">成绩</label>
                        <input type="text" class="form-control" id="score" name="score" placeholder="成绩">
                    </div>


                </form>
            </div>
            <div class="modal-footer">

                <button type="button" class="btn btn-primary" onclick="saveStudent();">保存</button>




            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->






<script src="/js/bootstrap.min.js"></script>
<script type="text/javascript">
    function preAdd(){

        $("#id").val(0);//提示新增
    }
    function deleteStudent(id){
        if (confirm("你是否真的要删除吗？")){
            $.ajax({
                url:"/webapi/student/delete/"+id,
                method:"delete"

            }).done(function(){
                loadTable();

            });
        }

    }






    function editStudent(id){
        $("#myModal").modal('show');
        $.ajax({
            url:'/webapi/student/get/'+id
        }).done(function(rs){

            $("#id").val(rs.id);
            $("#name").val(rs.name);
            $("#password").val("");
            $("#sex").val(rs.sex);
            $("#score").val(rs.score);

            })
    }

    function saveStudent(){

        var data=$("#formStudent").serialize();
        var id=$("#id").val();
        if (id<1){
            //是新增
            $.ajax({
                url:"/webapi/student/insert",
                method:"post",
                data:data
            }).done(function(){
                loadTable();
                $("#myModal").modal('hide');

            })
        }else{
            $.ajax({
                url:"/webapi/student/update",
                method:"put",
                data:data
            }).done(function(){
                loadTable();
                $("#myModal").modal('hide')

            })

        }
    }

    function loadTable(){
        $.ajax({
            url: '/webapi/student/list'
        }).done(function (rs) {
            var len=rs.length;
            var html="";

            for (var i = 0;i < len; i++) {
                var item = rs[i];
                html+="<tr class='table table-hover'>"
                    +"<td class='danger'>"+item.id+"</td>"
                    + "<td class='info'>"+item.no+"</td>"
                    + "<td class='success'>"+item.name+"</td>"
                    + "<td class='info'>"+item.sex+"</td>"
                    + "<td class='danger'>"+item.score+"</td>"
                    + "<td class='danger'><a href='#' onclick='editStudent("+item.id+");'>编辑</a> <a href='#' onclick='deleteStudent("+
                    +item.id+")'>删除</a>"+"</td>"



                    + "</tr>";

            }
            $("#studentTb").html(html);
        })
    }
    $(function () {
        loadTable();

    })
</script>
    <script th:inline="javascript">
        var student=[[${data}]]
        var len=student.length
        function find(){
            var p=document.getElementById("p1")
            var ss=document.getElementById("exampleInputEmail2").value
            var dd=""
            var f=0
            for (var i=0;i<len;i++)
            {
                if(ss==student[i].name||ss==student[i].no)
                {
                    dd= " 姓名:"+student[i].name+"  "
                        +" 学号:"+student[i].no+" "
                        +" 性别:"+student[i].sex+" "

                        +" 成绩:"+student[i].score+" "
                    p.innerHTML=dd
                    f=1
                    break;
                }
            }
            if(f==0)
            {
                dd="查无此人"
                p.innerHTML=dd
            }
        }
    </script>

</body>
</html>